package com.atguigu.tingshu;

import com.atguigu.tingshu.common.constant.RedisConstant;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;

@Slf4j
@SpringBootApplication//(scanBasePackages = {"cn.atguigu", "com.atguigu"})  //默认扫描到启动类所在包，扫描当前模块以及引入的jar
@EnableDiscoveryClient
@EnableFeignClients
@EnableScheduling  //开启定时任务
@EnableCaching //开启springCache
public class ServiceAlbumApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(ServiceAlbumApplication.class, args);
    }


    @Autowired
    private RedissonClient redissonClient;

    /**
     * CommandLineRunner接口springboot提供
     * 当springboot应用启动后自动执行一次
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        //1.初始化布隆过滤器 参数1：期望数据规模  参数2：误判率
        log.info("初始化布隆过滤器");

        //1.1 获取布隆过滤器对象
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        //1.2 初始化布隆过滤器（存在不处理（前提：未扩容），不存在则执行初始化）
        if (!bloomFilter.isExists()) {
            bloomFilter.tryInit(10000, 0.03);
        }
    }
}
